package com.taotao.manage.controller;

import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.service.ItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

/**
 * java类简单作用描述
 *
 * @ProjectName: taotao
 * @Package: com.taotao.manage.controller
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 作者姓名
 * @CreateDate: 2018/4/21 22:26
 * @UpdateUser: Neil.Zhou
 * @UpdateDate: 2018/4/21 22:26
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * <p>
 * Copyright: Copyright (c) 2018
 */
@Controller
@RequestMapping("item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> insertItemAndItemDesc(Item item, @RequestParam("desc") String desc){
        if (StringUtils.isEmpty(item.getTitle())){
            return ResponseEntity.badRequest().build();
        }
        try {
            itemService.insertItemAndItemDesc(item, desc);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping
    public ResponseEntity<EasyUIResult> queryItemListByPage(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                                                            @RequestParam(value = "rows",defaultValue = "10")Integer pageSize){
        try {
            PageInfo<Item> itemPageInfo = itemService.queryPageListByWhereOrderBy("updated desc", pageNum, pageSize);
            if (CollectionUtils.isEmpty(itemPageInfo.getList())){
                return ResponseEntity.notFound().build();
            }
            EasyUIResult result = new EasyUIResult(itemPageInfo.getTotal(),itemPageInfo.getList());
            return ResponseEntity.ok().body(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updateItemAndItemCat(Item item, @RequestParam("desc") String desc){
        try {
            boolean flag = itemService.updateItemAndItemDesc(item, desc);
            if (flag){
                return ResponseEntity.noContent().build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();


    }
}
